<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Urunler extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    public function __construct()
    {
        parent::__construct();
            // Your own constructor code
        $this->load->library('MyCategories');   
        
    } 
	public function index()
	{

                $data['categories'] = $this->config_model->get_categories()->result();
                $data['subview'] = 'urunler/view_kategoriler';
                $this->load->view('layouts/default2',$data);
        
	}
    public function urun($product_name,$product_id)
    {
        redirect('urun/'.$product_name.'/'.$product_id);
    }
    public function show_products($category_name = NULL, $subcategory_name = NULL, $brand_name = NULL)
    {
        
        if($category_name != NULL AND $subcategory_name == NULL AND $brand_name == NULL)
        {
            $category_id = $this->mycategories->get_category_name($this->config_model->get_categories()->result(),$category_name);
        
            $subcategories = $this->config_model->get_subcategories($category_id);
            if($subcategories->num_rows()> 0)
            {
                $data['category_id'] = $category_id;
                $data['category_name'] = $this->config_model->get_category_name($category_id)->row(0)->name;
               
                $data['subcategories'] = $this->config_model->get_subcategories($category_id)->result();
                
                $data['subview'] = 'urunler/list_categories';
                $this->load->view('layouts/default_listing',$data);
               
                
            }
            else
            {
                echo "urunler line 57";
            }
        }
        else if( $category_name != NULL AND $subcategory_name != NULL AND ($brand_name == NULL OR is_numeric($brand_name) ))
        {
                   
            $category_id = $this->mycategories->get_category_name($this->config_model->get_categories()->result(),$category_name);
            $subcategory_id = $this->mycategories->get_subcategory_name($this->config_model->get_subcategories($category_id)->result(),$subcategory_name);
            
            $subcategories = $this->config_model->get_subcategories($category_id);
            
            if($subcategories->num_rows()> 0)
            {
                $category_id = $this->mycategories->get_category_name($this->config_model->get_categories()->result(),$category_name);
                $subcategory_id = $this->mycategories->get_subcategory_name($this->config_model->get_subcategories($category_id)->result(),$subcategory_name);
              //  $brand_id = $this->mycategories->get_brand_name($this->config_model->get_brands()->result(),$brand_name);
              
                $data['category_id'] = $category_id;
                $data['category_name'] = $this->config_model->get_category_name($category_id)->row(0)->name;
                
                $data['subcategory_id'] = $subcategory_id;
                if($this->config_model->get_subcategory_name($subcategory_id)->num_rows() > 0)
                {
                  $data['subcategory_name'] = $this->config_model->get_subcategory_name($subcategory_id)->row(0)->name;  
                }
                else
                {
                    $data['subcategory_name'] = null;
                }
                
                
               // $data['brand_id'] = $brand_id;
              /*  if($this->config_model->get_brand_name($brand_id)->num_rows()> 0)
                {
                       $data['brand_name'] = $this->config_model->get_brand_name($brand_id)->row(0)->name;
                }
                else
                {
                        $data['brand_name'] = null;
                }*/
                
                $pag_url = base_url().'urunler/'.$category_name."/".$subcategory_name;
                $per_page = $this->_pagination2($category_id,$subcategory_id,$pag_url);
               // $data['list_products'] = $this->_capture_products($category_id,$subcategory_id,$brand_id, $per_page,$this->uri->segment(6));
                
                
                $this->db->select('*');
                $this->db->from('products');
               // $this->db->join('seller', 'seller.product_id = products.product_id');
                $this->db->where(array('subcategory_id' => $subcategory_id, 'category_id' => $category_id ));
              //  $this->db->where('seller.mod <>',2);
                 if($this->session->userdata('sort_by'))
               {
                $this->db->order_by($this->session->userdata('sort_by'),$this->session->userdata('sort_order'));
               }
                $this->db->limit($per_page,$this->uri->segment(4));
                
                $qq = $this->db->get()->result();
                $this->db->select('*');
                $this->db->from('products');
                
                $this->db->where(array('subcategory_id' => $subcategory_id, 'category_id' => $category_id ));
                $qq2 = $this->db->group_by("brand_id")->get()->result();
                $data['brands'] = $qq2; 
                $data['pagination'] = $this->pagination->create_links();
                
                $data['products'] = $qq;
              
                $data['subview'] = 'urunler/list_products2';
                $this->load->view('layouts/default_listing',$data);
            }
            else
            {
                echo "line 99 yok";
               
            }            
            
        }
        else if($category_name != NULL AND $subcategory_name != NULL AND $brand_name != NULL)
        {
                $category_id = $this->mycategories->get_category_name($this->config_model->get_categories()->result(),$category_name);
                $subcategory_id = $this->mycategories->get_subcategory_name($this->config_model->get_subcategories($category_id)->result(),$subcategory_name);
                $brand_id = $this->mycategories->get_brand_name($this->config_model->get_brands()->result(),$brand_name);
              
                $data['category_id'] = $category_id;
                $data['category_name'] = $this->config_model->get_category_name($category_id)->row(0)->name;
                
                $data['subcategory_id'] = $subcategory_id;
                if($this->config_model->get_subcategory_name($subcategory_id)->num_rows() > 0)
                {
                  $data['subcategory_name'] = $this->config_model->get_subcategory_name($subcategory_id)->row(0)->name;  
                }
                else
                {
                    $data['subcategory_name'] = null;
                }
                
                
                $data['brand_id'] = $brand_id;
                if($this->config_model->get_brand_name($brand_id)->num_rows()> 0)
                {
                       $data['brand_name'] = $this->config_model->get_brand_name($brand_id)->row(0)->name;
                }
                else
                {
                        $data['brand_name'] = null;
                }
                
                $pag_url = base_url().'urunler/'.$category_name."/".$subcategory_name."/".$brand_name;
                $per_page = $this->_pagination($category_id,$subcategory_id,$brand_id,$pag_url);
               // $data['list_products'] = $this->_capture_products($category_id,$subcategory_id,$brand_id, $per_page,$this->uri->segment(6));
                
                
                $this->db->select('*');
                $this->db->from('products');
               // $this->db->join('seller', 'seller.product_id = products.product_id');
                $this->db->where(array('subcategory_id' => $subcategory_id, 'category_id' => $category_id , 'brand_id' => $brand_id));
              //  $this->db->where('seller.mod <>',2);
                $this->db->limit($per_page,$this->uri->segment(5));
                  if($this->session->userdata('sort_by'))
               {
                $this->db->order_by($this->session->userdata('sort_by'),$this->session->userdata('sort_order'));
               }
                $qq = $this->db->get()->result();
                $data['pagination'] = $this->pagination->create_links();
   
                $data['products'] = $qq;
              
                $data['subview'] = 'urunler/list_products';
                $this->load->view('layouts/default_listing',$data);   
        }
        else
        {
            $data['categories'] = $this->config_model->get_categories()->result();
            $data['subview'] = 'urunler/view_kategoriler';
            $this->load->view('layouts/default3',$data);
        }
    }

    public function _pagination($category_id,$subcategory_id,$brand_id,$page_url)
    {
     
  		
		$config['base_url'] = $page_url;
        
                   $this->db->select('*');
                $this->db->from('products');
           //     $this->db->join('seller', 'seller.product_id = products.product_id');
             //   $this->db->where('seller.mod <>',2);
                $this->db->where(array('subcategory_id' => $subcategory_id, 'category_id' => $category_id , 'brand_id' => $brand_id));
               // $this->db->limit($per_page,$this->uri->segment(5));
            
                $qq = $this->db->get()->num_rows();        
	   
            
        
    
        $config['total_rows'] =  $qq; //$this->db->where(array('brand_id' => $brand_id , 'category_id' => $category_id , 'subcategory_id' => $subcategory_id ))->get('products')->num_rows();
		$config['per_page'] = 12;
	
        $config['uri_segment'] = '5';
        //$config['page_query_string'] = TRUE;
       
        $config['next_link'] = 'Sonraki';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_link'] = 'Önceki';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';        
        
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        

        
		$config['full_tag_open'] = ' <div class="pagination pull-right"><ul>';
		$config['full_tag_close'] = '</ul></div>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
	
		$this->pagination->initialize($config);
        return $config['per_page'];
		
        
		
    }    
  public function _pagination2($category_id,$subcategory_id,$page_url)
    {
  		
		$config['base_url'] = $page_url;
        
                   $this->db->select('*');
                $this->db->from('products');
           //     $this->db->join('seller', 'seller.product_id = products.product_id');
             //   $this->db->where('seller.mod <>',2);
                $this->db->where(array('subcategory_id' => $subcategory_id, 'category_id' => $category_id ));
               // $this->db->limit($per_page,$this->uri->segment(5));
                $qq = $this->db->get()->num_rows();        
	   
            
        
    
        $config['total_rows'] =  $qq; //$this->db->where(array('brand_id' => $brand_id , 'category_id' => $category_id , 'subcategory_id' => $subcategory_id ))->get('products')->num_rows();
		$config['per_page'] = 12;
	
        $config['uri_segment'] = '4';
        //$config['page_query_string'] = TRUE;
       
        $config['next_link'] = 'Sonraki';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_link'] = 'Önceki';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';        
        
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        

        
		$config['full_tag_open'] = ' <div class="pagination pull-right"><ul>';
		$config['full_tag_close'] = '</ul></div>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
	
		$this->pagination->initialize($config);
        return $config['per_page'];
		
        
		
    }       
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */